import Vue from 'vue'
import Vuex from 'vuex'
import createPersistedState from "vuex-persistedstate";
Vue.use(Vuex)

const Types={
  SET_TOKEN:"SET_TOKEN",
  SET_USER:"SET",
  SET_USERavatar:"SETAVATAR",
  SET_USERrealName:"SETRealName"
}

const state={
  isToken:false,
  user:{}
} 

const getters={
  isToken:state=>state.isToken,
  user:state=>state.user
}

const mutations={
  [Types.SET_TOKEN](state,payload){
    if(payload){
      state.isToken=payload
    }else{
      state.isToken=false
    }
  },
  [Types.SET_USER](state,payload){
    state.user.username=payload.username,
    state.user.avatar=payload.avatar,
    state.user.status=payload.status,
    state.user.id=payload.id,
    state.user.realname=payload.realname
  },
  [Types.SET_USERavatar](state,payload){
    state.user.avatar=payload
  },
  [Types.SET_USERrealName](state,payload){
    state.user.realname=payload
  },
}

const actions={
  setUser({commit},payload){
    commit(Types.SET_USER,payload)
  },
  setUserAvatar({commit},payload){
    commit(Types.SET_USERavatar,payload)
  },
  setUserRealName({commit},payload){
    commit(Types.SET_USERrealName,payload)
  },
  setToken({commit},payload){
    commit(Types.SET_TOKEN,payload)
  }
}
export default new Vuex.Store({
  state,
  mutations,
  getters,
  actions,
  plugins: [createPersistedState()]
})
